/*
 * Copyright (C) 2007-2008 SlideMe client
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.slideme.android.manager.respoitory;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.util.Log;
import org.slideme.android.manager.discovery.ContentBundle;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

/**
 * Provides methods for the storing and retreiving of content bundles.
 */
public final class ContentBundleRepository {

    private ContentBundleContentProvider provider;

    private static Uri uri = Uri.parse("content://org.slideme/content_bundle");

    public Cursor fetchNotInstalledAsCursor() {
        return provider.queryForAllUninstalledApps(Uri.parse("content://org.slideme/content_bundle"));
    }

    public Cursor fetchCursorById(int id) {
        return provider.queryById(uri, id);
    }

    public Cursor fetchCursorByInstalledBundleIds(List<String> bundleIds) {
        return provider.queryByBundleIds(uri, bundleIds, true);
    }

    public void deleteByBundleId(String bundleId) throws IOException {
        provider.delete(Uri.parse("content://org.slideme/content_bundle/" + bundleId), null, null);
    }

    public void store(List<ContentBundle> contentBundles) throws IOException {
        if (contentBundles == null) {
            throw new IllegalArgumentException("contentBundles");
        }
        for (ContentBundle contentBundle : contentBundles) {
            store(contentBundle);
        }
    }

    public void store(ContentBundle contentBundle) throws IOException {
        if (contentBundle == null) {
            throw new IllegalArgumentException("contentBundle");
        }
        Log.i("Storing Content Bundle:", contentBundle.getBundleId());
        try {
            new URI(contentBundle.getContentURI());
        } catch (URISyntaxException e) {
            Log.i("ContentBundleRepository", "Could not store content: URI For content bundle incorrect: URI = "
                    + contentBundle.getContentURI() + ", BundleId = " + contentBundle.getBundleId());
            return;
        }
        ContentValues contentValues = new ContentValues();
        contentValues.put("bundleId", contentBundle.getBundleId());
        contentValues.put("displayName", contentBundle.getDisplayName());
        contentValues.put("shortDescription", contentBundle.getShortDescription());
        contentValues.put("longDescription", contentBundle.getLongDescription());
        contentValues.put("vendorName", contentBundle.getVendorName());
        contentValues.put("price", contentBundle.getPrice());
        contentValues.put("iconFileUri", contentBundle.getIconFileUri());
        contentValues.put("termsAndConditions", contentBundle.getTermsAndConditions());
        contentValues.put("contentURI", contentBundle.getContentURI());
        contentValues.put("version", contentBundle.getVersion());
        contentValues.put("category", contentBundle.getCategory());
        contentValues.put("creationDate", contentBundle.getCreationDate());
        contentValues.put("isInstalled", "false");

        try {
            provider.insert(uri, contentValues);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public ContentBundleContentProvider getContentBundleContentProvider() {
        return provider;
    }

    public void init(ContentBundleContentProvider provider) throws IOException {
        if (provider == null) {
            throw new IllegalArgumentException("provider");
        }

        if (!provider.onCreate()) {
            throw new IOException("Failed to create content bundle provider");
        }

        this.provider = provider;
    }
}
